using Senparc.Weixin.Exceptions;
using Senparc.Weixin.QY.Entities;
using Senparc.Weixin.QY.Helpers;
using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace Senparc.Weixin.QY
{
	public static class RequestMessageFactory
	{
		public static IRequestMessageBase GetRequestEntity(XDocument doc)
		{
			RequestMessageBase requestMessageBase = null;
			if (doc.Root.Element("MsgType") != null)
			{
				try
				{
					RequestMsgType requestMsgType = MsgTypeHelper.GetRequestMsgType(doc);
					switch (requestMsgType)
					{
					case RequestMsgType.Text:
						requestMessageBase = new RequestMessageText();
						goto IL_257;
					case RequestMsgType.Location:
						requestMessageBase = new RequestMessageLocation();
						goto IL_257;
					case RequestMsgType.Image:
						requestMessageBase = new RequestMessageImage();
						goto IL_257;
					case RequestMsgType.Voice:
						requestMessageBase = new RequestMessageVoice();
						goto IL_257;
					case RequestMsgType.Video:
						requestMessageBase = new RequestMessageVideo();
						goto IL_257;
					case RequestMsgType.Event:
					{
						string key;
						switch (key = doc.Root.Element("Event").Value.ToUpper())
						{
						case "CLICK":
							requestMessageBase = new RequestMessageEvent_Click();
							goto IL_257;
						case "VIEW":
							requestMessageBase = new RequestMessageEvent_View();
							goto IL_257;
						case "SUBSCRIBE":
							requestMessageBase = new RequestMessageEvent_Subscribe();
							goto IL_257;
						case "UNSUBSCRIBE":
							requestMessageBase = new RequestMessageEvent_UnSubscribe();
							goto IL_257;
						case "SCANCODE_PUSH":
							requestMessageBase = new RequestMessageEvent_Scancode_Push();
							goto IL_257;
						case "SCANCODE_WAITMSG":
							requestMessageBase = new RequestMessageEvent_Scancode_Waitmsg();
							goto IL_257;
						case "PIC_SYSPHOTO":
							requestMessageBase = new RequestMessageEvent_Pic_Sysphoto();
							goto IL_257;
						case "PIC_PHOTO_OR_ALBUM":
							requestMessageBase = new RequestMessageEvent_Pic_Photo_Or_Album();
							goto IL_257;
						case "PIC_WEIXIN":
							requestMessageBase = new RequestMessageEvent_Pic_Weixin();
							goto IL_257;
						case "LOCATION_SELECT":
							requestMessageBase = new RequestMessageEvent_Location_Select();
							goto IL_257;
						case "LOCATION":
							requestMessageBase = new RequestMessageEvent_Location();
							goto IL_257;
						case "ENTER_AGENT":
							requestMessageBase = new RequestMessageEvent_Enter_Agent();
							goto IL_257;
						case "BATCH_JOB_RESULT":
							requestMessageBase = new RequestMessageEvent_Batch_Job_Result();
							goto IL_257;
						}
						requestMessageBase = new RequestMessageEventBase();
						goto IL_257;
					}
					case RequestMsgType.ShortVideo:
						requestMessageBase = new RequestMessageShortVideo();
						goto IL_257;
					}
					throw new UnknownRequestMsgTypeException(string.Format("MsgType：{0} 在RequestMessageFactory中没有对应的处理程序！", requestMsgType), new ArgumentOutOfRangeException());
					IL_257:
					requestMessageBase.FillEntityWithXml(doc);
					return requestMessageBase;
				}
				catch (ArgumentException inner)
				{
					throw new WeixinException(string.Format("RequestMessage转换出错！可能是MsgType不存在！，XML：{0}", doc.ToString()), inner);
				}
			}
			if (doc.Root.Element("InfoType") != null)
			{
				try
				{
					ThirdPartyInfo thirdPartyInfo = MsgTypeHelper.GetThirdPartyInfo(doc);
					switch (thirdPartyInfo)
					{
					case ThirdPartyInfo.SUITE_TICKET:
						requestMessageBase = new RequestMessageInfo_Suite_Ticket();
						break;
					case ThirdPartyInfo.CHANGE_AUTH:
						requestMessageBase = new RequestMessageInfo_Change_Auth();
						break;
					case ThirdPartyInfo.CANCEL_AUTH:
						requestMessageBase = new RequestMessageInfo_Cancel_Auth();
						break;
					default:
						throw new UnknownRequestMsgTypeException(string.Format("InfoType：{0} 在RequestMessageFactory中没有对应的处理程序！", thirdPartyInfo), new ArgumentOutOfRangeException());
					}
					requestMessageBase.FillEntityWithXml(doc);
					return requestMessageBase;
				}
				catch (ArgumentException inner2)
				{
					throw new WeixinException(string.Format("RequestMessage转换出错！可能是MsgType和InfoType都不存在！，XML：{0}", doc.ToString()), inner2);
				}
			}
			throw new WeixinException(string.Format("RequestMessage转换出错！可能是MsgType和InfoType都不存在！，XML：{0}", doc.ToString()));
		}
		public static IRequestMessageBase GetRequestEntity(string xml)
		{
			return RequestMessageFactory.GetRequestEntity(XDocument.Parse(xml));
		}
		public static IRequestMessageBase GetRequestEntity(Stream stream)
		{
			IRequestMessageBase requestEntity;
			using (XmlReader xmlReader = XmlReader.Create(stream))
			{
				XDocument doc = XDocument.Load(xmlReader);
				requestEntity = RequestMessageFactory.GetRequestEntity(doc);
			}
			return requestEntity;
		}
		public static EncryptPostData GetEncryptPostData(string xml)
		{
			if (xml == null)
			{
				return null;
			}
			EncryptPostData encryptPostData = new EncryptPostData();
			encryptPostData.FillEntityWithXml(XDocument.Parse(xml));
			return encryptPostData;
		}
	}
}
